﻿using System;
using System.Xml.Serialization;

// ReSharper disable UnusedMember.Global

namespace LightPersist
{
	public struct BackReference
	{
		// Properties - for xml serializetion
		// Non autoproperties for allowing to initialize it in struct ctor

		Guid _id;

		string _propertyName;

		public BackReference(Guid id, string propertyName)
		{
			_id = id;
			_propertyName = propertyName;
		}

		[XmlAttribute]
		public Guid ID
		{
			get { return _id; }
			set { _id = value; }
		}

		[XmlAttribute]
		public string PropertyName
		{
			get { return _propertyName; }
			set { _propertyName = value; }
		}

		public override string ToString()
		{
			return "BackRef: " + ID.ToStringDebug() + "'s " + PropertyName;
		}

		public override bool Equals(object obj)
		{
			if (ReferenceEquals(null, obj))
			{
				return false;
			}
			if (obj.GetType() != typeof(BackReference))
			{
				return false;
			}
			return Equals((BackReference)obj);
		}

		public bool Equals(BackReference other)
		{
			return other._id.Equals(_id) && Equals(other._propertyName, _propertyName);
		}

		public override int GetHashCode()
		{
			unchecked
			{
				return (_id.GetHashCode() * 397) ^ (_propertyName != null ? _propertyName.GetHashCode() : 0);
			}
		}

		public static bool operator ==(BackReference a, BackReference b)
		{
			return a.Equals(b);
		}

		public static bool operator !=(BackReference a, BackReference b)
		{
			return !(a == b);
		}
	}
}